#include "DeleteCommand.h"
#include <iostream>

// @author A0096955J

DeleteCommand::DeleteCommand(string inputSentence){
	storage=Storage::getInstance();
	restOfCommand = inputSentence;
	parser(restOfCommand);
}
DeleteCommand::~DeleteCommand(void){
}

string DeleteCommand::Execute()
{
	return storage->deleteTask(index);
}

string DeleteCommand::Unexecute()
{
	storage->insertTask(index,previousTask);
	return CommonConstant::MESSAGE_DELETE_UNDO_SUCCESSFULLY;
}

void DeleteCommand::parser(string inputSentence)
{
	if(inputSentence.empty()){
		throw CommonConstant::MESSAGE_NO_USER_INPUT_AFTER_COMMAND;
	}
	
	inputSentence.erase(inputSentence.begin(),inputSentence.begin()+1);
	
	if(isValidNumber(inputSentence))
	{
		if(storage->retrieveTask(stoi(inputSentence,nullptr,10)) == NULL)
		{
			throw storage->deleteTask(index);
		}
		else
		{
			index = stoi(inputSentence,nullptr,10);
			previousTask = storage->retrieveTask(index);
		}
	} else {
		throw CommonConstant::MESSAGE_INTEGER_VALUES_ONLY;
	}
}
